package epam.concurrency.lab2.task1;

import java.util.concurrent.Callable;

/**
 * Callable sin-cos finder for the multi-threaded SinCos Summer
 * @author Iurii Miedviediev
 * @version 1.0 Build 14.08.2014
 */
public class SinCosSummer implements Callable<Double> {

    //bounds to find sin within
    private final double begin;
    private final double end;
    private static final double STEP = 0.0001;

    //variable to accumulate results
    private Double result = 0.0;

    public SinCosSummer(double begin, double end) {
        this.begin = begin;
        this.end = end;
    }

    @Override
    public Double call() throws Exception {
        for(double num = this.begin; num <= this.end; num = SinCosRangeAccumulator.customRound(num + STEP)) {
            //round to 0.0001 to prevent big offsets
            result += SinCosRangeAccumulator.customRound(function(num));
        }
        return result;
    }

    private double function(double num) {
        return Math.sin(num) * Math.cos(num);
    }
}
